<script setup lang="ts">
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home';
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue';
import PageSkeleton from './components/PageSkeleton.vue'
import HotPanel from './components/HotPanel.vue';
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import type { BannerItem, CategoryItem, HotItem } from '@/types/home';
import type { XtxGuessInstance } from '@/types/component';


const bannerList = ref<BannerItem[]>([])
//获取轮播图数据
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  //console.log(res);
  bannerList.value = res.result

}
//获取前台分类数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  //console.log(res);
  categoryList.value = res.result

}
//获取热门分类数据
const hotPanelList = ref<HotItem[]>([])
const getHomeHotPanelData = async () => {
  const res = await getHomeHotAPI()
  hotPanelList.value = res.result

}
// 获取猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>()

// 滚动触底事件
const onScrolltolower = () => {
  guessRef.value?.getMore()
}
// 下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData()
  // 加载数据
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotPanelData(), guessRef.value?.getMore()])
  // 关闭动画
  isTriggered.value = false
}
//是否加载，显示骨架屏
const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotPanelData()])
  isLoading.value = false
})
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <!-- 滚动容器 -->
  <scroll-view class="scrill-view" refresher-enabled @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered" scroll-y @scrolltolower="onScrolltolower">
    <!-- 骨架屏 -->
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 自定义轮播图 -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="hotPanelList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </template>

  </scroll-view>
</template>

<style lang="scss">
//
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;

  .scrill-view {
    flex: 1;
  }
}
</style>
